package com.example.administrator.yuejian_2.fragment;


<<<<<<< HEAD
import android.content.Intent;
=======
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
<<<<<<< HEAD
import android.widget.ImageButton;
import android.widget.ImageView;
=======
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
import android.widget.RadioGroup;
import android.widget.ViewFlipper;

import com.example.administrator.yuejian_2.R;
<<<<<<< HEAD
import com.example.administrator.yuejian_2.activity.AcDetailActivity;
import com.example.administrator.yuejian_2.activity.SearchActivity;
import com.example.administrator.yuejian_2.bean.ActivityPicBean;
import com.example.administrator.yuejian_2.interservice.ActivityService;
import com.example.administrator.yuejian_2.interservice.Const;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
=======
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {
<<<<<<< HEAD
    ViewFlipper viewFlipper;
    private RadioGroup radioGroup;
    private AcFragment activityFragment;
    private MasterFragment masterFragment;
    private ContestFragment contestFragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    private ImageButton searchButton;
    private ImageView img1,img2,img3,img4;
=======
//    ViewFlipper viewFlipper;
//
//    private RadioGroup radioGroup;
//
//    private AcFragment activityFragment;
//    private MasterFragment masterFragment;
//    private ContestFragment contestFragment;
//
//    private FragmentManager fragmentManager;
//    private FragmentTransaction fragmentTransaction;
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //在Fragment中添加Fragment时，调用getChildFragmentManager获取FragmentManager
<<<<<<< HEAD
          fragmentManager = getChildFragmentManager();
        // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_home, container, false);
=======
      //  fragmentManager = getChildFragmentManager();
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2

    }
    public void onViewCreated(View view, @Nullable Bundle saveInstanceState){

<<<<<<< HEAD
        viewFlipper= view.findViewById(R.id.flipper);
//        viewFlipper.startFlipping();

        img1 = view.findViewById(R.id.img1);
        img2 = view.findViewById(R.id.img2);
        img3 = view.findViewById(R.id.img3);
        img4 = view.findViewById(R.id.img4);

        getpic();

        radioGroup = view.findViewById(R.id.radioGroup_home);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.activity:
                        activityFragment = new AcFragment();
                        replaceFragment(activityFragment);
                        break;
                    case R.id.master:
                        masterFragment = new MasterFragment();
                        replaceFragment(masterFragment);
                        break;
                    case R.id.contest:
                        contestFragment = new ContestFragment();
                        replaceFragment(contestFragment);
                        break;

                }
            }
        });
        radioGroup.check(R.id.activity);


        searchButton = view.findViewById(R.id.home_search);
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                getActivity().startActivity(intent);
            }
        });

    }

    private void replaceFragment(Fragment fragment){

        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_holder_home,fragment);
        fragmentTransaction.commit();

    }

    private void getpic(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ActivityService service = retrofit.create(ActivityService.class);
        Call<List<ActivityPicBean>> call = service.activitypic();
        call.enqueue(new Callback<List<ActivityPicBean>>() {
            @Override
            public void onResponse(Call<List<ActivityPicBean>> call, Response<List<ActivityPicBean>> response) {
                if (getActivity() == null)
                    return;

                  ActivityPicBean i = response.body().get(0);
                  final String name_one= i.getA_name();
                  final int cost_one = i.getA_money();
                  final int aid_one = i.getAid();
                  String u_one = i.getA_pic();
                  Picasso.with(getActivity())
                            .load("http://10.0.2.2/YueJian16/Public/activityphoto/"+u_one)
                            .into(img1);
                    img1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(getActivity(), AcDetailActivity.class);
                            intent.putExtra("aid",aid_one);
                            intent.putExtra("cost",cost_one);
                            intent.putExtra("activityName",name_one);
                            getActivity().startActivity(intent);
                        }
                    });


                ActivityPicBean ii = response.body().get(1);
                final String name_two= ii.getA_name();
                final int cost_two = ii.getA_money();
                final int aid_two = ii.getAid();
                String u_two = ii.getA_pic();
                Picasso.with(getActivity())
                        .load("http://10.0.2.2/YueJian16/Public/activityphoto/"+u_two)
                        .into(img2);
                img2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), AcDetailActivity.class);
                        intent.putExtra("aid",aid_two);
                        intent.putExtra("cost",cost_two);
                        intent.putExtra("activityName",name_two);
                        getActivity().startActivity(intent);

                    }
                });

                ActivityPicBean iii = response.body().get(2);
                final String name_three= iii.getA_name();
                final int cost_three = iii.getA_money();
                final int aid_three = iii.getAid();
                String u_three = iii.getA_pic();
                Picasso.with(getActivity())
                        .load("http://10.0.2.2/YueJian16/Public/activityphoto/"+u_three)
                        .into(img3);
                img3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), AcDetailActivity.class);
                        intent.putExtra("aid",aid_three);
                        intent.putExtra("cost",cost_three);
                        intent.putExtra("activityName",name_three);
                        getActivity().startActivity(intent);
                    }
                });

                ActivityPicBean iiii = response.body().get(3);
                final String name_four= iiii.getA_name();
                final int cost_four = iiii.getA_money();
                final int aid_four = iiii.getAid();
                String u_four = iiii.getA_pic();
                Picasso.with(getActivity())
                        .load("http://10.0.2.2/YueJian16/Public/activityphoto/"+u_four)
                        .into(img4);
                img4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), AcDetailActivity.class);
                        intent.putExtra("aid",aid_four);
                        intent.putExtra("cost",cost_four);
                        intent.putExtra("activityName",name_four);
                        getActivity().startActivity(intent);
                    }
                });

                viewFlipper.startFlipping();

            }

            @Override
            public void onFailure(Call<List<ActivityPicBean>> call, Throwable t) {

            }
        });
=======
//        viewFlipper= view.findViewById(R.id.flipper);
//        viewFlipper.startFlipping();
//
//        radioGroup = view.findViewById(R.id.radioGroup_home);
//        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                switch (checkedId){
//                    case R.id.activity:
//                        activityFragment = new AcFragment();
//                        replaceFragment(activityFragment);
//                        break;
//                    case R.id.master:
//                        masterFragment = new MasterFragment();
//                        replaceFragment(masterFragment);
//                        break;
//                    case R.id.contest:
//                        contestFragment = new ContestFragment();
//                        replaceFragment(contestFragment);
//                        break;
//
//                }
//            }
//        });
//        radioGroup.check(R.id.activity);
//
//    }
//
//    private void replaceFragment(Fragment fragment){
//
//        fragmentTransaction = fragmentManager.beginTransaction();
//        fragmentTransaction.replace(R.id.fragment_holder_home,fragment);
//        fragmentTransaction.commit();
//
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
    }

}
